package fakeopentypedialog;

import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaUIMessages;
import org.eclipse.jdt.internal.ui.dialogs.OpenTypeSelectionDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

@SuppressWarnings("restriction")
public class FakeOpenTypeContainer {
    OpenTypeSelectionDialog dialog = null;
    Display display = null;
    Control c = null;
    Button b = null;
    
    private static FakeOpenTypeContainer handle = getInstance();
    public static FakeOpenTypeContainer getInstance() {
	if (handle==null) handle = new FakeOpenTypeContainer();
	return handle;
    }
    private FakeOpenTypeContainer() {
	
    }
    public void showDialog() {
	Shell parent = JavaPlugin.getActiveWorkbenchShell();
	dialog = new OpenTypeSelectionDialog(parent, true, PlatformUI.getWorkbench()
		.getProgressService(), null, IJavaSearchConstants.TYPE);
	dialog.setTitle(JavaUIMessages.OpenTypeAction_dialogTitle);
	dialog.setMessage(JavaUIMessages.OpenTypeAction_dialogMessage);

	try {	    	  
	    dialog.setBlockOnOpen(false);
	    dialog.open();
	    c = dialog.getPatternControl();
	    display = c.getShell().getDisplay();
	    
	    try {
		Thread.sleep(1500);
	    } catch (InterruptedException ie) {
	    }
	    new Thread() {
		public void run() {
		    postString("Test");		   		    
		}
	    }.start();

	} catch (Exception e) {
	    e.printStackTrace();
	}	
    }
    
    public void postEnter() {
	b = (Button) dialog.getOkButton();
	display.syncExec(
		new Runnable() {
		    public void run() {
			b.forceFocus();
		    }
		}
	);
	
	Event event = new Event();
	event.type = SWT.KeyDown;
	event.item = dialog.getOkButton();
	event.character = '\r';
	display.post(event);
    }
    public void postString(String string) {
	int min = 300;
	int max = 1000;

	
	for (int i = 0; i < string.length(); i++) {
	    display.syncExec(
			new Runnable() {
			    public void run() {
				c.forceFocus();
			    }
			}
		);
	    
	    char ch = string.charAt(i);
	    boolean shift = Character.isUpperCase(ch);
	    ch = Character.toLowerCase(ch);
	    if (shift) {
		Event event = new Event();
		event.type = SWT.KeyDown;
		event.keyCode = SWT.SHIFT;			
		display.post(event);
	    }
	    Event event = new Event();
	    event.type = SWT.KeyDown;
	    event.item = (Text) dialog.getPatternControl();
	    event.character = ch;
	    display.post(event);
	    try {
		Thread.sleep(10);
	    } catch (InterruptedException e) {
	    }
	    event.type = SWT.KeyUp;
	    display.post(event);
	    try {
		Thread.sleep(min + (int) (Math.random() * ((max - min) + 1)));
	    } catch (InterruptedException e) {
	    }
	    if (shift) {
		event = new Event();
		event.type = SWT.KeyUp;
		event.keyCode = SWT.SHIFT;
		display.post(event);
	    }
	}

    }
}
